﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System.IO;

namespace RandomPic
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        private static Version TargetedVersion = new Version(7, 10, 8858);
        public static bool IsTargetedVersion { get { return Environment.OSVersion.Version >= TargetedVersion; } }

        public MainPage()
        {
            InitializeComponent();
        }

        void photo1ChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                //MessageBox.Show(e.ChosenPhoto.Length.ToString());
                //Pic1.Source = e.ChosenPhoto;
                //Code to display the photo on the page in an image control named myImage.
                System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                bmp.SetSource(e.ChosenPhoto);

                saveImage((BitmapSource)bmp, "/Shared/ShellContent/pic1.jpg");
                Pic1.Source = bmp; 
            }
        }

        
        // modify Application Tile data
        private void updateTile(Uri imgUri)
        {
            // some random number
           // Random random = new Random();

            StandardTileData data = new StandardTileData();

            // get application tile
            ShellTile tile = ShellTile.ActiveTiles.First();
            if (tile == null)
            {
                ShellTile.Create(new Uri("/MainPage.xaml", UriKind.Relative), data);
            }
            
                // create a new data for tile
                //StandardTileData data = new StandardTileData();
                // tile foreground data
                data.Title = "";
                data.BackgroundImage = imgUri;
                data.Count = 0;
                // to make tile flip add data to background also
                data.BackTitle = "Tile Image";
                data.BackBackgroundImage = imgUri;
                data.BackContent = "";
                // update tile
                tile.Update(data);
            
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask photoChooserTask;
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photo1ChooserTask_Completed);
            photoChooserTask.Show();
        }

        private void saveImage(BitmapSource bmpsource, string imgName)
        {
            try
            {
                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isf.FileExists(imgName))
                        isf.DeleteFile(imgName);
                    using (IsolatedStorageFileStream isfs = isf.CreateFile(imgName))
                    {
                        var bmp = new WriteableBitmap(bmpsource);
                        bmp.SaveJpeg(isfs, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
        //private Uri getImage(string imgName)
        //{
        //    BitmapImage Bit_Img = new BitmapImage();
        //    using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
        //    {
        //        using (IsolatedStorageFileStream FS = ISF.OpenFile(imgName, FileMode.Open, FileAccess.Read))
        //        {
        //            Bit_Img.SetSource(FS);
        //            this.Pic1.Height = Bit_Img.PixelHeight;
        //            this.Pic1.Width = Bit_Img.PixelWidth;
        //        }
        //    }
        //    this.Pic1.Source = Bit_Img;
        //    var uri = new Uri("ms-appdata:///Local/" + imgName, UriKind.Absolute);
        //    return uri;
        //}

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //BitmapImage bmp = (BitmapImage)Pic1.Source;
            ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("MainPage.xaml"));

            // Create the tile if we didn't find it already exists.
            // Create the tile object and set some initial properties for the tile.
            // The Count value of 12 will show the number 12 on the front of the Tile. Valid values are 1-99.
            // A Count value of 0 will indicate that the Count should not be displayed.
            //var uri = new Uri("WP_000725.jpg", UriKind.Absolute);
            StandardTileData NewTileData = new StandardTileData
            {
                BackgroundImage = new Uri("isostore:/Shared/ShellContent/pic1.jpg", UriKind.Absolute),
                Title = txtImgtext.Text,
                Count = 0,
                BackTitle = "",
                BackContent = "",
                BackBackgroundImage = new Uri("isostore:/Shared/ShellContent/pic1.jpg", UriKind.Absolute)
            };
            if (TileToFind == null)
            {
                // Create the tile and pin it to Start. This will cause a navigation to Start and a deactivation of our application.
                Uri tilesUri = new Uri("/MainPage.xaml", UriKind.Relative);
                ShellTile.Create(tilesUri, NewTileData);
            }
            else
            {
                TileToFind.Update(NewTileData);
                MessageBox.Show("Your Image Tiles is updated");
            }

        }

    }
}